import json
import os

import settings

# allure 报告的路径
allure_json_file = os.path.join(settings.ALLURE_REPORT_DIR, "widgets", "summary.json")


class AllureReport:
    def __init__(self):
        if not os.path.exists(allure_json_file):
            raise FileNotFoundError("allure 报告未生成或路径错误")
        with open(allure_json_file, "r", encoding="utf-8") as f:
            self.summary = json.load(f)

    @property
    def passed_count(self) -> int:
        return self.summary['statistic']['passed']

    @property
    def failed_count(self) -> int:
        return self.summary['statistic']['failed']

    @property
    def broken_count(self) -> int:
        return self.summary['statistic']['broken']

    @property
    def skipped_count(self) -> int:
        return self.summary['statistic']['skipped']

    @property
    def total_count(self) -> int:
        return self.summary['statistic']['total']

    @property
    def pass_rate(self):
        if self.total_count != 0:
            return round(self.passed_count / self.total_count * 100, 2)
        return 0

    @property
    def process_duration(self):
        # 执行总时长
        return round(self.summary['time']['duration'] / 1000, 2)

